//
//  SearchHistoryView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/10/25.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "SearchHistoryView.h"

@interface SearchHistoryView()

@property(nonatomic,strong)UIButton *btn;

@property(nonatomic,strong)UILabel *noLabel;

@property(nonatomic,strong)NSMutableArray *btnArray;

@end

@implementation SearchHistoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self  = [super initWithFrame:frame]) {

        
    }
    return self;
}

- (void)setHistoryArray:(NSMutableArray *)historyArray
{
    _historyArray = historyArray;
//    NSLog(@"---1-1-1-1-%zd",self.historyArray.count);
    
    
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:14];
    nameLabel.text = @"历史搜索";
    nameLabel.textColor = [UIColor grayColor];
    [self addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.left.equalTo(self.mas_left).offset(15);
    }];
    
    
    UIButton *deleteBtn = [[UIButton alloc] init];
    [deleteBtn setImage:[UIImage imageNamed:@"gouwuche_1"] forState:UIControlStateNormal];
    [self addSubview:deleteBtn];
    [deleteBtn addTarget:self action:@selector(deleteSearchHistoryClick) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(10);
        make.right.equalTo(self.mas_right).offset(-15);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];

    [_btn removeFromSuperview];
    if (_historyArray.count > 0) {
       
        [_noLabel removeFromSuperview];
        
    }else{
        _noLabel = [[UILabel alloc] init];
        _noLabel.font = [UIFont systemFontOfSize:14];
        _noLabel.text = @"暂无历史记录";
        _noLabel.textColor = [UIColor blackColor];
        [self addSubview:_noLabel];
        [_noLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.centerY.mas_equalTo(20);
        }];
    }
    
    
    //删除历史记录
    if (_isDelete_History) {
       for (UIButton *tempBtn in self.btnArray) {
            [tempBtn removeFromSuperview];
            
        }
    }
    
    int count = 0;
    float btnWidth = 0;
    float btnMargin = 10;

    self.btnArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < _historyArray.count; ++i) {
        self.btn = [[UIButton alloc] init];
        [self addSubview:_btn];
        [_btn addTarget:self action:@selector(SearchHistoryBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    
        [_btn setTitle:[NSString stringWithFormat:@"%@",_historyArray[i]] forState:UIControlStateNormal];
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_btn setBackgroundColor:BACKGROUND_COLOR];
        _btn.titleLabel.font = FONT(14);
        
        NSString *str = _historyArray [i];
        _btn.tag = i;
        NSDictionary *dict = [NSDictionary dictionaryWithObject:FONT(15) forKey:NSFontAttributeName];
        CGSize btnSize = [str sizeWithAttributes:dict];
        _btn.width = btnSize.width + WIDTH/20;
        _btn.Height = btnSize.height + btnMargin;
        
        [self.btnArray addObject:_btn];
        
        if (i == 0) {
            _btn.x = btnMargin;
            btnWidth += CGRectGetMaxX(_btn.frame);
        }else{
            btnWidth += CGRectGetMaxX(_btn.frame)+20;
            if (btnWidth > WIDTH) {
                count ++;
                _btn.x = btnMargin;
                btnWidth = CGRectGetMaxX(_btn.frame);
            }else{
                _btn.x += btnWidth - _btn.width;
            }
        }
        
        _btn.y += count *(_btn.Height+btnMargin)+40;
        
    }
    


}

//删除历史记录
- (void)deleteSearchHistoryClick
{
    if ([self.delegate respondsToSelector:@selector(SearchHistorydeleteClick)]) {
        [self.delegate SearchHistorydeleteClick];
    }
}

//点击历史记录某一条
- (void)SearchHistoryBtnClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(SearchHistoryDelegateClick:)]) {
        [self.delegate SearchHistoryDelegateClick:sender.tag];
    }
}


@end
